// static/scripts.js

const themeToggle = document.getElementById('theme-toggle');
let currentTheme = localStorage.getItem('theme') || 'light';

document.documentElement.setAttribute('data-theme', currentTheme);
updateButtonText();

function updateButtonText() {
    const currentTheme = document.documentElement.getAttribute('data-theme');
    themeToggle.textContent = `Toggle to ${currentTheme === 'dark' ? 'Light' : 'Dark'}`;
}

themeToggle.addEventListener('click', () => {
    const switchToTheme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'light' : 'dark';
    updateTheme(switchToTheme);
});

function updateTheme(theme) {
    document.documentElement.setAttribute('data-theme', theme);
    localStorage.setItem('theme', theme);
    updateButtonText();
}

function togglePasswordVisibility(serverId) {
    var passwordField = document.getElementById('password-' + serverId);
    var toggleIcon = document.getElementById('toggle-password-' + serverId);
    
    if (passwordField.type === 'password') {
      passwordField.type = 'text';
      toggleIcon.classList.add('show-password');
    } else {
      passwordField.type = 'password';
      toggleIcon.classList.remove('show-password');
    }
}


function checkTool(toolName) {
    fetch(`/check-tool?tool=${encodeURIComponent(toolName)}`)
        .then(response => response.json())
        .then(data => {
            if (!data.exists) {
                // 弹出错误框
                alert(`${toolName} is not installed on your system. Please install it first.`);
                showDownloadButton();
            } else {
                // 工具已安装，隐藏下载按钮
                document.getElementById('download-button').style.display = 'none';
            }
        })
        .catch(error => {
            console.error('Error:', error);
            // 弹出错误框
            alert('An error occurred while checking for the tool. Please try again later.');
            showDownloadButton();
        });
}

function getOsType() {
    const userAgent = navigator.userAgent.toLowerCase();

    if (userAgent.includes('win')) {
        return 'Windows';
    } else {
        return 'Linux'; // 默认为 Linux
    }
}

// 调用函数以在页面加载时执行检查
window.onload = checkIpmitool;